from django.db import models
from mlh.utils.models import BaseModel
from users.models import User

# Create your models here.


class Label(BaseModel):
    """标签表"""

    parent = models.ForeignKey('self', on_delete=models.SET_NULL, verbose_name='上级标签', null=True, blank=True)
    name = models.CharField(max_length=20, verbose_name='标签名字')
    content = models.TextField(null=True, blank=True, verbose_name='标签内容')
    number = models.IntegerField(default=0, verbose_name='被关注数目')

    class Meta:
        db_table = 'tb_label'
        verbose_name = '标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Qusetions(BaseModel):
    """问题表"""
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='提问用户')
    browse = models.IntegerField(default=0, verbose_name='浏览量')
    content = models.TextField(null=True, blank=True, verbose_name='提问内容')
    title = models.CharField(max_length=50, verbose_name='问题标题')
    label = models.ForeignKey(Label, on_delete=models.CASCADE, verbose_name='标签', null=True)
    like_count = models.IntegerField(default=0, verbose_name='点赞数')

    class Meta:
        db_table = 'tb_ask'
        verbose_name = '问题'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Answer(BaseModel):

    """回答表"""
    respondent = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='回答用户')
    question = models.ForeignKey(Qusetions, on_delete=models.CASCADE, related_name='subs', verbose_name='回答问题')
    content = models.TextField(null=True, blank=True, verbose_name='回答内容')

    class Meta:
        db_table = 'tb_answer'
        verbose_name = '回答'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.question.title


class Likes(BaseModel):
    """点赞有用表"""
    answer = models.ForeignKey(Answer, null=True, blank=True, on_delete=models.CASCADE, verbose_name='点赞回答')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='点赞用户')

    class Meta:
        db_table = 'tb_likes'
        verbose_name = '点赞有用'
        verbose_name_plural = verbose_name


class LabeltoQusetions(BaseModel):

    label = models.ForeignKey(to='Label', to_field="id")
    question = models.ForeignKey(to='Qusetions', to_field="id")

    class Meta:
        db_table = 'tb_labeltoquestion'
        verbose_name = '标签和问题对应表'
        verbose_name_plural = verbose_name